#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_AUDIO_EQ		(0)
#define ITEM_AUDIO_BEEP		(1)
#define ITEM_AUDIO_FAD		(2)
#define ITEM_AUDIO_BAS		(3)
#define ITEM_AUDIO_EXIT		(4)

TRect		MenuAudioTouchRect[IMG_MENU_AUDIO_ITEM_QTY];
TPosCoor	MenuAudioItemPos[IMG_MENU_AUDIO_ITEM_QTY] = { {142,88}, {142,229}, {365,88}, {365,229} ,{576,376}};
int		MenuAudioFocus = -1;
TFrame		MenuAudioFrame;
//unsigned char MenuAudio_data [5] = {0xe0,0x03,0x06,0x02,0xeb};//select eq
extern  unsigned char send_data_buf[5];
char*		MenuAudioInfoStr[IMG_MENU_AUDIO_ITEM_QTY] = {"EQ","BEEP OFF","FAD/BAL","BAS/TRE","EXIT"};
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern int 	BeepFlag;



int  AudioInfoUpdata ()
{
}

void MenuAudioDrawBackgroup(TCanvas *pCanvas)
{
	//DrawJpeg(pCanvas, 0, 0, "/res_jpeg/menu_audio.jpg" );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "AUDIO SET" );

}

void MenuAudioDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;

	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
						
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "VIDEO DEFAULT" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);
	printf("mute_status is %d____\n",mute_status);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuAudioDrawVolume(MenuAudioFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
				printf ("in dis-display of volume~!\n");
			}
}


void MenuAudioDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;	
	char FileName[20];
	int GetJpegIdx;
	int offset =0;

	if( idx < 0 || idx >= IMG_MENU_AUDIO_ITEM_QTY )
		return;
	if (idx == ITEM_AUDIO_EXIT)
		offset = 1;
	
	
	if( bfocus ){
		GetJpegIdx = IMG_MENU_AUDIO_ITEM_FOCUS +offset;
	}else{
		GetJpegIdx = IMG_MENU_AUDIO_ITEM +offset;
	}
	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_AUDIO_SET_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	pos = MenuAudioItemPos [idx];
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	MenuAudioTouchRect[idx].left = pos.x;
	MenuAudioTouchRect[idx].right = pos.x + pJpeg.Width;
	MenuAudioTouchRect[idx].top = pos.y;
	MenuAudioTouchRect[idx].bottom = pos.y + pJpeg.Height;
	
	if ((idx == ITEM_AUDIO_BEEP) && (BeepFlag ==0))
		{
		printf ("draw beep on !beepflag is %d,idx is %d\n",BeepFlag,idx);
		g_Puts( pCanvas, FONT33, MenuAudioTouchRect[idx], TEXT_ALIGN_CENTER, "BEEP ON");
		}
	else
		{
		printf ("draw beep off !beepflag is %d,idx is %d\n",BeepFlag,idx);
		g_Puts( pCanvas, FONT33, MenuAudioTouchRect[idx], TEXT_ALIGN_CENTER, MenuAudioInfoStr[idx]);
		}

	
}

void MenuAudioDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		MenuAudioDrawBackgroup(MenuAudioFrame.pCanvas);
	for( i = 0; i < IMG_MENU_AUDIO_ITEM_QTY; i++ )
		MenuAudioDrawItem( MenuAudioFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
			MenuAudioDrawVolume(MenuAudioFrame.pCanvas,1);	
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int MenuAudioGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MENU_AUDIO_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MenuAudioTouchRect[i] ) )
			return i;
	return -1;
}
//must be changed .some bug exit.
BOOL MenuAudioBtnEnter(int idx)
{
	switch(idx)
	{
		case ITEM_AUDIO_EQ:
			AudioEQInit();
			break;
		case ITEM_AUDIO_BEEP:
			if (BeepFlag)
				{
				BeepFlag = 0;							
				}
			else
				BeepFlag = 1;		
			MenuAudioDrawItem( MenuAudioFrame.pCanvas, idx, FALSE );
			dt_Present_part(MenuAudioTouchRect [idx]);
			break;
		case ITEM_AUDIO_BAS:
			AudioBASInit();
			break;
		case ITEM_AUDIO_FAD:
			AudioFADInit();
			break;
		case ITEM_AUDIO_EXIT:
			MenuMainInit();
			break;
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}

void MenuAudioInit()
{
	ir_SetCurrentID( IR_MENU_AUDIO_INIT );

	frm_Create( &MenuAudioFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MenuAudioDraw( TRUE );
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
						switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
										read_info_data[0] = 0;			
																									
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuAudioDrawVolume(MenuAudioFrame.pCanvas,1);	
								}									
								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:
										MenuAudioDrawVolume(MenuAudioFrame.pCanvas,1);	
										break;
									case	ID_DMUTE:
										MenuAudioDrawVolume(MenuAudioFrame.pCanvas,3);	
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									case 	ID_FMAM:
										//band command;
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
										read_info_data[0] = 0;						 
						break;
						
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;			
						break;	
					
					default:	
							rc = -1;						
						}
	return 0;
						
}
/*
static int send_data_out ( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_EQ_ITEM_QTY))
		return -1;
	if (index == ITEM_AUDIO_EXIT)
		return 0;
		
		switch (index)
		{
			case ITEM_AUDIO_EQ:
				MenuAudio_data [2] = 0x06;
				MenuAudio_data [3] = 0x01;
				MenuAudio_data [4] = 0xea;
				break;
			case ITEM_AUDIO_BEEP:
				MenuAudio_data [2] = 0x0e;
				if (!BeepFlag)
					{
					MenuAudio_data [3] = 0x01;
					MenuAudio_data [4] = 0xf2;
					}
				else
					{
					MenuAudio_data [3] = 0x02;
					MenuAudio_data [4] = 0xf3;
					}
				break;	
			case	ITEM_AUDIO_BAS:
				MenuAudio_data [2] = 0x06;
				MenuAudio_data [3] = 0x08;
				MenuAudio_data [4] = 0xf1;
				break;
			case	ITEM_AUDIO_FAD:
				MenuAudio_data [2] = 0x06;
				MenuAudio_data [3] = 0x0d;
				MenuAudio_data [4] = 0xf6;
				break;
		}
		if (dev_SPIWrite(MenuAudio_data,5)==0)
			return 0;//hangsheng 071126
	return -1;
		
}
*/

static int send_data ( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_EQ_ITEM_QTY))
		return -1;
	if (index == ITEM_AUDIO_EXIT)
		return 0;
		
		switch (index)
		{
			case ITEM_AUDIO_EQ:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_EQ;
				break;
			case ITEM_AUDIO_BEEP:
				send_data_buf [2] = IDDATA_BEEP;
				if (!BeepFlag)
					{
					send_data_buf [3] = BEEP_ON;
					}
				else
					{
					send_data_buf [3] = BEEP_OFF;
					}
				break;	
			case	ITEM_AUDIO_BAS:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_BAS_SOURCE;
				break;
			case	ITEM_AUDIO_FAD:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_FAD_SOURCE;
				break;
		}

			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
	return -1;
}



void MenuAudioInput(UInt32 key)
{
	
		int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag = message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MenuAudioFocus = MenuAudioGetTouchID( pos )) != -1 )
				{
					if ( send_data(MenuAudioFocus))
					printf ("write failure !\n");	
					MenuAudioDrawItem( MenuAudioFrame.pCanvas, MenuAudioFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					//dt_Present();
					dt_Present_part(MenuAudioTouchRect [MenuAudioFocus]);
				}
			}
		}
		else
		{
			if( ( focus = MenuAudioGetTouchID( pos ) ) == MenuAudioFocus )
				MenuAudioBtnEnter( MenuAudioFocus );
			else if( MenuAudioFocus != -1 )
			{
				MenuAudioDrawItem( MenuAudioFrame.pCanvas, MenuAudioFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				//dt_Present();
				dt_Present_part(MenuAudioTouchRect [MenuAudioFocus]);
			}
			MenuAudioFocus = -1;
		}
		LastKey = key;//hangsheng add 071126		
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}


